using System;
using Microsoft.SPOT;
using Microsoft.SPOT.Presentation.Media;
using Microsoft.SPOT.Touch;
using Microsoft.SPOT.Input;

namespace Microsoft.SPOT.Presentation.Controls
{
    public class Button : ContentControl
    {
        private Border border;
        private bool isFocused, isPressed;
        private Brush background, borderBrush, pressedBackground, pressedBorderBrush;

        public event EventHandler Click;
        public event PropertyChangedEventHandler IsPressedChanged;

        #region Properties
        //Hide real child of this control from other classes   

        public new UIElement Child
        {
            get { return border.Child; }
            set
            {
                border.Child = value;
            }
        }
        public bool IsPressed
        {
            get { return isPressed; }
            set
            {
                if (value != isPressed)
                {
                    isPressed = value;
                    if (isPressed)
                    {
                        border.Background = PressedBackground;
                        border.BorderBrush = PressedBorderBrush;
                    }
                    else
                    {
                        border.Background = Background;
                        border.BorderBrush = BorderBrush;
                    }
                    OnIsPressedChanged(!isPressed, isPressed);
                }
            }

        }
        public new Brush Background
        {
            get { return background; }
            set
            {
                background = value;
                if (!IsPressed)
                {
                    border.Background = background;
                }
            }
        }
        public Brush BorderBrush
        {
            get { return borderBrush; }
            set
            {
                borderBrush = value;
                if (!IsPressed)
                {
                    border.BorderBrush = borderBrush;
                }
            }
        }
        public Brush PressedBackground
        {
            get { return pressedBackground; }
            set
            {
                pressedBackground = value;
                if (IsPressed)
                {
                    border.Background = value;
                }
            }
        }
        public Brush PressedBorderBrush
        {
            get { return pressedBorderBrush; }
            set
            {
                pressedBorderBrush = value;
                if (IsPressed)
                {
                    border.BorderBrush = value;
                }
            }
        }
        #endregion

        public Button(UIElement child)
        {
            border = new Border();

            //Default settings
            Background = new SolidColorBrush(Colors.White);
            BorderBrush = new SolidColorBrush(Colors.Black);
            PressedBackground = Background;
            PressedBorderBrush = new SolidColorBrush(Colors.Gray);
            SetBorderThickness(2);

            Child = child;
            base.Child = border;
        }

        public void SetBorderThickness(int thickness)
        {
            border.SetBorderThickness(thickness);
        }

        protected override void OnTouchDown(TouchEventArgs e)
        {
            base.OnTouchDown(e);
            isFocused = TouchCapture.Capture(this);
            IsPressed = isFocused;
        }
        protected override void OnTouchMove(TouchEventArgs e)
        {
            base.OnTouchMove(e);
            if (isFocused)
            {
                int x, y;
                e.GetPosition(this, e.Touches.Length - 1, out x, out y);
                IsPressed = x >= 0 && x <= ActualWidth && y >= 0 && y <= ActualHeight; //is the current position inside the button?
            }
        }
        protected override void OnTouchUp(TouchEventArgs e)
        {
            base.OnTouchUp(e);
            if (isFocused)
            {
                int x, y;
                IsPressed = false;
                isFocused = !TouchCapture.Capture(this, CaptureMode.None);
                e.GetPosition(this, 0, out x, out y);
                if (x >= 0 && x <= ActualWidth && y >= 0 && y <= ActualHeight)
                { //TouchUp was inside the button
                    OnClick();
                }
            }
        }

        protected virtual void OnIsPressedChanged(bool oldValue, bool newValue)
        {
            if (IsPressedChanged != null)
            {
                IsPressedChanged(this, new PropertyChangedEventArgs("IsPressed", oldValue, newValue));
            }
        }
        protected virtual void OnClick()
        {
            if (Click != null)
            {
                Click(this, new EventArgs());
            }
        }
    }
}